import {defineStore} from 'pinia'
import {UmsparentListRes} from '@/type/ums'
import type { MenuOption } from 'naive-ui'
const useMenuStore = defineStore('menu', {
  state:():{
    menu: MenuOption[]
  } => ({
    // 菜单
    menu: []
  }),
  actions: {
    setMenu(data:UmsparentListRes[]) {
      function transformData(arr:UmsparentListRes[]):MenuOption[] {
        const result = arr
          .map((item): MenuOption | undefined => {
            const lastIndex = item.menu_url.lastIndexOf('/');
            const key = item.menu_url.substring(lastIndex + 1);
            if (item.menu_hidden === "1") {
              const obj: MenuOption = {
                label: item.menu_name,
                key: key,
              };

              if (item.children?.length) {
                obj.children = transformData(item.children);
              }
              return obj;
            }
            return undefined;
          })
          .filter(item => item !== undefined) as MenuOption[];
        return  result
      }
      this.menu = transformData( data )
    }
  },
  getters: {}
})

export default useMenuStore
